package com.yidu.ruifeng_logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.yidu.ruifeng_logistics.entity.LogisticsModel;
import com.yidu.ruifeng_logistics.entity.LogisticsRole;
import com.yidu.ruifeng_logistics.entity.LogisticsRoleModel;
import com.yidu.ruifeng_logistics.service.LogisticsModelService;
import com.yidu.ruifeng_logistics.service.LogisticsRoleModelService;
import com.yidu.ruifeng_logistics.service.LogisticsRoleService;
import com.yidu.ruifeng_logistics.utils.BaseMessage;

import com.yidu.ruifeng_logistics.vo.LogisticsRoleModelVo;
import com.yidu.ruifeng_logistics.vo.ModulTreeNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (LogisticsRoleModel)表控制层
 *
 * @author 张皓超
 * @since 2021-03-09 14:29:12
 */
@Api(description = "角色模块接口")
@RestController
@RequestMapping("logisticsRoleModel")
public class LogisticsRoleModelController {

    Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 服务对象
     */
    @Resource
    private LogisticsRoleModelService logisticsRoleModelService;

    /**
     * 模块
     */
    @Resource
    private LogisticsModelService logisticsModelService;

    /**
     * 角色
     */
    @Resource
    private LogisticsRoleService logisticsRoleService;
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public LogisticsRoleModel selectOne(Integer id) {
        return this.logisticsRoleModelService.queryById(id);
    }


    @PostMapping("show")
    public BaseMessage show(Integer id) {

        Map<String, Object>map=new HashMap<>();
        LogisticsRoleModel logisticsRoleModel = this.logisticsRoleModelService.queryById(id);
        map.put("data1",logisticsRoleModel);

        List<LogisticsRoleModel> logisticsRoleModels = logisticsRoleModelService.queryRoleIdReturnRoleModelId(logisticsRoleModel.getRoleId());
        map.put("data2",logisticsRoleModels);
        return BaseMessage.success(map);
    }


    @ApiOperation(value = "遍历员工", notes = "返回BaseMessage")
    @GetMapping("selectAll")
    public Map<String, Object> selectAll(Integer page,Integer limit){
        //将参数传入到分页助手
        PageHelper.startPage(page,limit);
        List<LogisticsRoleModelVo> logisticsRoleModelVos = logisticsRoleModelService.selectAllVo();
        Map<String, Object> map = new HashMap<>();
        PageInfo<LogisticsRoleModelVo> pageInfo = new PageInfo<>(logisticsRoleModelVos);
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("data",pageInfo.getList());//可以直接写list
        map.put("count",pageInfo.getTotal()); //总行数
        return map;
    }

    @ApiOperation(value = "增加修改", notes = "返回BaseMessage")
    @PostMapping("addAndUpdate")
    public BaseMessage addAndUpdate(Integer roleModelId,Integer roleId,String name){
        int i= logisticsRoleModelService.addAndUpdate(roleModelId,roleId,name);
        return i>0?BaseMessage.success():BaseMessage.fail();
    }

    @ApiOperation(value = "拿到角色选择框所需的值", notes = "返回BaseMessage")
    @PostMapping("selectBox")
    public BaseMessage selectBox(){
        List<LogisticsRole> logisticsRoles = logisticsRoleService.queryAll(new LogisticsRole());
        return BaseMessage.success(logisticsRoles);
    }

    @ApiOperation(value = "拿到树形组件的数据", notes = "返回BaseMessage")
    @PostMapping("showData")
    public BaseMessage showData(){
        List<LogisticsModel> logisticsModels = logisticsModelService.selectParentIsNull();
        List<ModulTreeNode>modulTreeNodes=new ArrayList<>();
        for (LogisticsModel model:logisticsModels) {
            ModulTreeNode modulTreeNode=new ModulTreeNode();
            modulTreeNode.setId(model.getModelId());
            modulTreeNode.setTitle(model.getModelName());
            modulTreeNode.setChecked(false);
            modulTreeNode.setSpread(false);
            modulTreeNode.setField("name");
            modulTreeNode.setChildren(selectF(model.getModelId()));
            modulTreeNodes.add(modulTreeNode);
        }

        return BaseMessage.success(modulTreeNodes);
    }

    /**
     * 树形递归
     *
     * @param modelId
     * @return
     */
    public List<ModulTreeNode> selectF(Integer modelId) {

        List<ModulTreeNode> list = new ArrayList<>();
        List<LogisticsModel> modelList = logisticsModelService.selectII(modelId);
        for (int i = 0; i < modelList.size(); i++) {
            ModulTreeNode modulTreeNode=new ModulTreeNode();
            modulTreeNode.setId(modelList.get(i).getModelId());
            modulTreeNode.setTitle(modelList.get(i).getModelName());
            modulTreeNode.setChecked(false);
            modulTreeNode.setSpread(false);
            modulTreeNode.setField("");
            if (modelId != modelList.get(i).getModelParent()) {
                return list;
            } else {
                modulTreeNode.setChildren(selectF(modelList.get(i).getModelId()));//递归
                list.add(modulTreeNode);
            }
        }

        return list;
    }


}
